/**
 * 新建图文和加载图文 包括文章/图文的判断（新建/编辑 图文/文章）
 */
define( [
  'jquery',
  'require',
  'bigwe_utils',
  'message',
  '../../modules/createEditor',
  '../../lib/utils',
  '../../lib/dataHandle',
  'api',
  'ueditor',
  '../../lib/editData',
  'article_import'
], function( $, require, bu ) {

  var ShowTitleMessage = require( 'message' );
  var editorObj = require( '../../modules/createEditor' );
  var editData = require( '../../lib/editData' );
  var utils = require( '../../lib/utils' );
  var api = require( 'api' );
  var dh = require( '../../lib/dataHandle' );
  var ue = require( 'ueditor' );
  var ai = require( 'article_import' );

  var showTime = ShowTitleMessage.showTime;
  var thisApi = api.EDIT_TEXT;

  // 判断是图文相关还是文章相关1是文章 其他是图文
  var editType = bu.getUrlParam( 'edit_type' ),
    articleGroup = bu.getUrlParam( 'article_group' ),
    cd = bu.getUrlParam( 'cd' ),
    tid = bu.getUrlParam( 'template_id' ),
    remotoUrl = $( '#fm_data #remotoUrl' ).val(),
    wordHandleResult = $( '#fm_data #word_handle_result' ).val(),
    promiseParam = [ editorReady( $.Deferred() ) ],
    contentType = -1;
  var contentHandleArray = [
    function( jsonObj ) {
      if ( !jsonObj.length || !jsonObj || jsonObj.error ) {
        ShowTitleMessage.showWarning( jsonObj.msg || '图文数据出现错误，当前为新建图文模式', showTime );
        return;
      }

      editData.isCreate = false;
      editData.articleGroup = articleGroup;

      utils.graghListCreateJsons( jsonObj, true );
    },
    function( data ) {
      var noData = '文章数据发生错误！当前为新建文章模式';
      if ( !data.data || !data.data.id ) {
        ShowTitleMessage.showWarning( data.msg || noData, showTime );
        return;
      }
      if ( data.ret ) {
        ShowTitleMessage.showWarning( data.msg || noData, showTime );
        return;
      }
      if ( data.data.original_author ) {
        data.data.declare_original = 1;
      }
      data = dh.articleObjTransition( data.data, true );

      if ( data.authorName ) {
        data.declare_original = 1;
      }

      editData.isCreate = false;
      utils.graghListCreateJsons( [ data ], true );

      editData.articleID = cd;
    },
    function( data ) {
      if ( !data.ret ) {
        $.extend( editData, {
          isCreate: false,
          isContentChange: false
        } );
        editorObj.setContent( data.data.content || '' );
      } else {
        ShowTitleMessage.showWarning( data.msg || '模板数据发生错误！无法载入模板', showTime );
      }
    },
    function( data ) {
      $.extend( editData, {
        isCreate: false,
        isContentChange: false
      } );
      utils.remotoHtmlCallback.call( editorObj, data.data.content, remotoUrl );
      ShowTitleMessage.show( "抓取成功(ˇ▽ˇ)(ˇ▽ˇ)(ˇ▽ˇ)，尽情编辑吧~", showTime );
    }
  ];

  function editorReady( dtd ) {

    editorObj.ready( function() {
      if ( wordHandleResult ) {
        $.extend( editData, {
          isCreate: false,
          isContentChange: false
        } );
        this.setContent( unescape( wordHandleResult ) );
      }
      // 该异步对象完成
      dtd.resolve();
    } );
    // 承诺的，外部无法改变其状态
    return dtd.promise();
  }

  // 数据初始化载入
  if ( editType == 1 ) {
    if ( cd ) {
      editData.isCreate = false;
      ShowTitleMessage.show( '正在载入文章数据' );
      promiseParam.push( $.get( thisApi.GET_ART_INFO, {
        "cd": cd
      } ) );
      contentType = 1;
    } else if ( tid ) {
      editData.isCreate = false;
      ShowTitleMessage.show( '正在载入模板数据' );
      promiseParam.push( $.get( api.TEMPLATE.GET_TEMPLATE, {
        "tid": tid
      } ) );
      contentType = 2;
    } else if ( remotoUrl ) { // 抓取远程的微信文章
      editData.isCreate = false;
      contentType = 3;
      if ( ai.check( remotoUrl ) ) {
        promiseParam.push( ai.doRemotoRequest( remotoUrl ) );
      } else {
        return this;
      }
    }
  } else {
    if ( articleGroup ) {
      editData.isCreate = false;
      ShowTitleMessage.show( '正在载入图文数据' );
      promiseParam.push( $.get( thisApi.GET_EDIT_CHART, {
        'article_group': articleGroup
      } ) );
      contentType = 0;
    }
  }
  // 保证编辑器和异步请求都加载完成后再填充数据到编辑器
  $.when.apply( this, promiseParam ).then( function( arg, arg2 ) {
    editData.allIsReady = !0;
    // 取得响应对象，为数组的第一个下标 response
    var repData = arg2 ? arg2[ 0 ] : '';
    if ( repData ) {
      ShowTitleMessage.hide();
      response = typeof repData == 'object' ? repData : JSON.parse( repData );
      contentHandleArray[ contentType ].call( this, response );
    }

  } );

  // 不要删除此处
  // this.AGFunArray[editType != '1' ? '0' : '1']();
} );